package server;

import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDB extends DB {
    public UserDB(final String DBName, final String tableName, final String varStr) {
        super(DBName, tableName, varStr);
    }

    public boolean register(final String username, final String password){
        User user = getUserMessage(username);
        if(user != null) return false;
        if (currentTable == null)
            return false;
        try {
            final String sql = "insert into " + currentTable + " (name,password,plan,speed) values(" + "\'" + username
                    + "\'" + ",\'" + password + "\'" + ",\'" + "cet4:0" + "\'" + ",\'" + "cet4:0" + "\'" + ")";
            final Statement st = con.createStatement();
            int ret = st.executeUpdate(sql);
            st.close();
            return ret!=0? true:false;
        } catch (final SQLException e) {
            return false;
        }
    }

    public boolean login(final String username, final String password){
        if (currentTable == null)
            return false;
        try {
            final String sql = "select * from " + currentTable + " where name=\"" + username + "\"";
            final Statement st = con.createStatement();
            final ResultSet rSet = st.executeQuery(sql);
            String sb="";
            while (rSet.next()) {
                rSet.getString(1);
                sb=rSet.getString(2);
                rSet.getString(3);
                rSet.getString(4);
            }
            rSet.close();
            st.close();
            return sb.equals(password);
        } catch (SQLException e) {
            return false;
        }
    }
    public boolean add(final Object user){
        if (currentTable == null)
            return false;
        try {
            String name, plan, speed;
            plan = "";
            speed = "";
            name = ((User)user).getName() ;
            HashMap<String,List<Integer>> Schedules = ((User) user).getSchedules();
            Iterator<Map.Entry<String,List<Integer>>> iterator= Schedules.entrySet().iterator();
            boolean flag = false;
            while(iterator.hasNext()){
                if(flag){
                    plan = plan + ";";
                    speed = speed + ";";
                }
                Map.Entry<String,List<Integer>> entry = iterator.next();
                plan = plan + entry.getKey() + ":" + entry.getValue().get(1);
                speed = speed + entry.getKey() + ":" + entry.getValue().get(0);
                flag = true;
            }
            String password = "";
            final String sql = "insert into " + currentTable + " (name,password,plan,speed) values(" + "\'" + name
                    + "\'" + ",\'" + password + "\'" + ",\'" + plan + "\'" + ",\'" + speed + "\'" + ")";
            final Statement st = con.createStatement();
            boolean ret = st.execute(sql);
            st.close();
            return ret;
        } catch (final SQLException e) {
            return false;
        }
    }
    public boolean addUserStudySpeed(final String username,final String dictionaryname,final int amount) {
        User after = getUserMessage(username);
        Integer studied = getUserStudySpeed(username, dictionaryname);
        Integer daily_plan = getUserPlan(username, dictionaryname);
        after.addSchedules(dictionaryname, studied+amount, daily_plan);
        return update(username, after);
    }
    public boolean setUserPlan(final String username,final String dictionaryname,final int daily_plan){
        User after = getUserMessage(username);
        if(!after.getSchedules().keySet().contains(dictionaryname))
            after.addSchedules(dictionaryname, 0, daily_plan);
        else after.addSchedules(dictionaryname,after.getSchedules().get(dictionaryname).get(0),daily_plan);
        return update(username, after);
    }
    public boolean update(final Object before, final Object after){
        if (currentTable == null)
            return false;
        try {
            String name, plan, speed;
            plan = "";
            speed = "";
            name = (String) before;
            HashMap<String,List<Integer>> Schedules = ((User) after).getSchedules();
            Iterator<Map.Entry<String,List<Integer>>> iterator= Schedules.entrySet().iterator();
            boolean flag = false;
            while(iterator.hasNext()){
                if(flag){
                    plan = plan + ";";
                    speed = speed + ";";
                }
                Map.Entry<String,List<Integer>> entry = iterator.next();
                plan = plan + entry.getKey() + ":" + entry.getValue().get(1);
                speed = speed + entry.getKey() + ":" + entry.getValue().get(0);
                flag = true;
            }
            final String sql = "update " + currentTable + " set plan=\"" + plan + "\",speed=\"" + speed
                    + "\" where name=\"" + name + "\"";
            final Statement st = con.createStatement();
            st.executeUpdate(sql);
            st.close();
            return true;
        } catch (final SQLException e) {
            return false;
        }
    }
    public boolean delete(final Object person){
        if (currentTable == null)
            return true;
        try {
            final String sql = "delete from " + currentTable + " where name=\'" + ((String) person) + "\'";
            final Statement st = con.createStatement();
            st.executeUpdate(sql);
            st.close();
            return true;
        } catch (SQLException e) {
            return false;
        }
    }
    public int getUserStudySpeed(final String username,final String dictionaryname){
        User user = getUserMessage(username);
        return (user.getSchedules().get(dictionaryname)).get(0);
    }
    public int getUserPlan(final String username,final String dictionaryname){
        User user = getUserMessage(username);
        return (user.getSchedules().get(dictionaryname)).get(1);
    }
    public User getUserMessage(final String username){
        return (User) query(username);
    }
    @SuppressWarnings("unused")
    public Object query(final Object person){
        if (currentTable == null)
            return null;
        try {
            final String sql = "select * from " + currentTable + " where name=\"" + ((String) person) + "\"";
            final Statement st = con.createStatement();
            final ResultSet rSet = st.executeQuery(sql);
            User ret = null;
            while (rSet.next()) {
                ret = new User(rSet.getString(1));
                String sb=rSet.getString(2);
                String[] plan = rSet.getString(3).split(";");
                String[] speed = rSet.getString(4).split(";");
                for(int i=0;i<plan.length;i++){
                    String dic = plan[i].split(":")[0];
                    Integer studied = Integer.valueOf(speed[i].split(":")[1]);
                    Integer daily_plan = Integer.valueOf(plan[i].split(":")[1]);
                    ret.addSchedules(dic, studied, daily_plan);
                }
            }
            rSet.close();
            st.close();
            return ret;
        } catch (SQLException e) {
            return null;
        }
    }
}